package top.wilsonlv.jaguar.cloud.auth.extension;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import top.wilsonlv.jaguar.cloud.auth.extension.device.DeviceAuthenticationFilter;
import top.wilsonlv.jaguar.cloud.auth.extension.device.DeviceAuthenticationProvider;
import top.wilsonlv.jaguar.cloud.auth.extension.face.FaceAuthenticationFilter;
import top.wilsonlv.jaguar.cloud.auth.extension.face.FaceAuthenticationProvider;
import top.wilsonlv.jaguar.cloud.auth.extension.sms.SmsAuthenticationFilter;
import top.wilsonlv.jaguar.cloud.auth.extension.sms.SmsAuthenticationProvider;
import top.wilsonlv.jaguar.cloud.auth.extension.sms.SmsService;
import top.wilsonlv.jaguar.cloud.auth.properties.JaguarAuthProperties;
import top.wilsonlv.jaguar.cloud.auth.security.LoginFailureHandler;
import top.wilsonlv.jaguar.cloud.auth.security.LoginSuccessHandler;

/**
 * @author lvws
 * @since 2022/1/8
 */
@Order(150)
@Configuration
@RequiredArgsConstructor
public class ExtensionSecurityConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    private final JaguarAuthProperties jaguarAuthProperties;

    private final LoginSuccessHandler loginSuccessHandler;

    private final LoginFailureHandler loginFailureHandler;

    @Autowired(required = false)
    private FaceAuthenticationProvider faceAuthenticationProvider;

    @Autowired(required = false)
    private DeviceAuthenticationProvider deviceAuthenticationProvider;

    @Autowired(required = false)
    private SmsAuthenticationProvider smsAuthenticationProvider;

    @Autowired(required = false)
    private SmsService smsService;

    @Override
    public void configure(HttpSecurity http) {
        if (jaguarAuthProperties.getFaceAuthEnable()) {
            FaceAuthenticationFilter faceAuthenticationFilter = new FaceAuthenticationFilter(
                    http.getSharedObject(AuthenticationManager.class), loginSuccessHandler, loginFailureHandler);

            http.authenticationProvider(faceAuthenticationProvider)
                    .addFilterAfter(faceAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
        }
        if (jaguarAuthProperties.getDeviceAuthEnable()) {
            DeviceAuthenticationFilter deviceAuthenticationFilter = new DeviceAuthenticationFilter(
                    http.getSharedObject(AuthenticationManager.class), loginSuccessHandler, loginFailureHandler);

            http.authenticationProvider(deviceAuthenticationProvider)
                    .addFilterAfter(deviceAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
        }
        if (jaguarAuthProperties.getSms().getEnable()) {
            SmsAuthenticationFilter smsAuthenticationFilter = new SmsAuthenticationFilter(smsService,
                    http.getSharedObject(AuthenticationManager.class), loginSuccessHandler, loginFailureHandler);

            http.authenticationProvider(smsAuthenticationProvider)
                    .addFilterAfter(smsAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
        }
    }

}
